using System;
using System.Collections.Generic;
using System.Xml;

namespace XmlObjects.Parsers
{
	/// <summary>
	/// Responsible for reading and writing objects that are based on a list of XmlElements with simple string values.
	/// 
	/// What do I need to know in order to funcation???
	/// 
	///     I need to know what Type(s) are in my list.
	///		I need the select string.
	/// 
	/// </summary>
	public class ListParser<T> : Parser<List<T>>
	{
		private readonly Parser<T> baseParser;

		public ListParser()
		{
			baseParser = GetLocalNodeParser<T>();
		}

		public override List<T> GetValue(XmlNode node)
		{
			List<T> theList = new List<T>();
			baseParser.Map = Map;
			
			foreach (XmlNode xmlNode in node.SelectNodes(SelectString))
			{
				theList.Add(baseParser.GetValueLocal(xmlNode));
			}
			return theList;
		}

		public override List<T> GetValueLocal(XmlNode node)
		{
			throw new NotImplementedException();
		}
//
//		public void WriteXml(List<T> theValue, XmlNode parentNode)
//		{
//			XmlDocument doc;
//			if (parentNode.OwnerDocument == null)
//				doc = (XmlDocument)parentNode;
//			else
//				doc = parentNode.OwnerDocument;
//
//			//  How to I make sure that I am in the right place?
//			//     Easy enough.
//			//  Get the path to the parent node of the elements.
//			XmlNode nodeToAddObjectsTo;
//			string[] paths = SelectString.Split('/');
//			string elementName = paths[paths.Length - 1];
//			if (paths.Length==1)
//			{
//				nodeToAddObjectsTo = parentNode;
//			}
//			else
//			{
//				XPathStore xPathStore = new XPathStore(doc);
//				string tempXpath = string.Join("/", paths, 0, paths.Length - 1);
//				parentNode = xPathStore.QueryCreatNode(tempXpath, parentNode);
//			}
//
//			foreach (T tObject in theValue)
//			{
//				XmlNode newNode = parentNode.AppendChild(doc.CreateElement(elementName));
//				baseParser.WriteXmlLocal(tObject, newNode);
//			}
//		}
	}
}